package com.suse.service.view.edu.service.impl;

import com.suse.common.config.response.ApiException;
import com.suse.common.config.response.R;
import com.suse.service.view.edu.model.entity.CourseCollect;
import com.suse.service.view.edu.mapper.CourseCollectMapper;
import com.suse.service.view.edu.service.CourseCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程收藏 服务实现类
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-08-16
 */
@Service
public class CourseCollectServiceImpl extends ServiceImpl<CourseCollectMapper, CourseCollect> implements CourseCollectService {

    @Override
    public R saveCollect(String courseId,String userId) {
        CourseCollect courseCollect = new CourseCollect();
        courseCollect.setCourseId(courseId);
        courseCollect.setUserId(userId);
        if (save(courseCollect)) {
            return R.success("已收藏");
        }else {
            throw new ApiException("收藏失败！");
        }
    }

    @Override
    public R removeCollect(String courseId, String userId) {
        boolean remove = lambdaUpdate()
                .eq(CourseCollect::getCourseId, courseId)
                .eq(CourseCollect::getUserId, userId)
                .remove();
        if (remove) {
            return R.success("已取消收藏");
        }else {
            throw new ApiException("取消收藏失败！");
        }
    }

}
